package com.haima.utils;

import com.wechat.pay.java.core.Config;
import com.wechat.pay.java.core.RSAAutoCertificateConfig;
import com.wechat.pay.java.core.util.PemUtil;
import com.wechat.pay.java.service.payments.jsapi.JsapiServiceExtension;
import com.wechat.pay.java.service.payments.jsapi.model.*;
import com.wechat.pay.java.service.payments.model.Transaction;
import org.springframework.core.io.ClassPathResource;

import java.io.IOException;
import java.security.PrivateKey;

public class WxPayTool {

    /** appId */
    public static String appId = WeChatConstant.APP_ID;
    /** 商户号 */
    public static String merchantId = WeChatConstant.MERCHANT_ID;
    /** 商户API私钥路径 */
    public static String privateKeyPath = WeChatConstant.PRIVATE_KEY_PATH;
    /** 商户证书序列号 */
    public static String merchantSerialNumber = WeChatConstant.MERCHANT_SERIALNUMBER;
    /** 商户APIV3密钥 */
    public static String apiV3Key = WeChatConstant.API_V3_KEY;

    public static JsapiServiceExtension service;

    public static ClassPathResource resourcePrivateKey = new ClassPathResource(privateKeyPath);

    public static PrivateKey merchantPrivateKey = PemUtil.loadPrivateKeyFromString("-----BEGIN PRIVATE KEY-----\n" +
            "MIIEvAIBADANBgkqhkiG9w0BAQEFAASCBKYwggSiAgEAAoIBAQDaVxx2leGFkbmk\n" +
            "u0vu8woRPTY8UhiGUsAy4fgPi1uttUsCuuuZEpYJxeTU1Ha5GvbVvX2LJn6UiQJ5\n" +
            "c0yTBsJpkGrKqXzAD3musPGxG+T1xAv3HwfIkRz7ePk/mLvAIITYwNa+TLNhS5IE\n" +
            "VDbbpNDGK5MER0kCIBTpD45YQoVzVeUIuZqh4OreZ4uQ7VgaO12T3XcJW2ZqDBI5\n" +
            "eoz4RHUotoBsB7Xwq8tIGBPn5hR8mXFT3mYnZj4hfFRN+dyMfKxldrSZeMt58QVf\n" +
            "wbACLk2xkXoUT1NdlnzZS+qH88lDnf0oO+iHCDOmfmD2YdtXTi9A244tnAnW/nzV\n" +
            "vexjf0vrAgMBAAECggEAVJkmFSiTvj4tcQ+FJ2uR/gX6qrqgeAM+Jzj49YTom9C/\n" +
            "5KnqOWBDNDwyIujDq4swErceGrlqmEDR7Z7oD5ux3lcvmaC8DmqwmihDLX2CZFt2\n" +
            "y2SBjovzb9h6stKKBLJU5uspCYkMg+lVBUpgCxC8N9yQVTsNpb4qe/ckDzyPGpV5\n" +
            "Lrlnsl8iiBMVKuPpscFBNtC3nGKahOs92mkptOuICdevf6Lfp14vPIr/or2egPhY\n" +
            "f6yCywDwdUbuO3xBye39UK4PtTKfYd4YdS/46eFibvnhpzMaQIwhrxqsb+v3Scgl\n" +
            "ws4FFIecUQQGXOdDU52nZtl/IOapa5VnbmurV9QEmQKBgQDxHTT+CwAuK+NtD1cI\n" +
            "EK/c/4weV00I0H0Aw+BwagCltIg9lJurN96ZGz3YiHe7rq6Mm7cRTlvoHqOUmDOW\n" +
            "8bvyHDLAonhvPOY1FZ9xWqb4UCUK2kcwtGQ8G+h6gKcoJMPu772du0v5oAo1btVM\n" +
            "YAYySMq4tq81MvYRhjoasCEtnwKBgQDn0fcsfsZQfYLoLVryX40GXtiYh9pLqNhI\n" +
            "UQGEOR/CX9QzD4gyVv4GV/BCHB/PNLM9HSkHz0tTrhuLO0d0JiRkH27080IBW9ON\n" +
            "tVisgQ34Eo18RXd4nAF8ewITLorpL41uN6YQnrRIspUKw+kvZPVPOBMIJoHIJbQB\n" +
            "qHaQd9zmNQKBgDoPI2SM95UmolPvnrBlfEOGEZJ4ySb3NTKK+C95hwBf0XKqpzV6\n" +
            "dZJ+mczbR6Gc9ZEMKe/z43OodRFh+QI+4VfybFIwjTmT+4cK1uLvxkcC3XiYiQ30\n" +
            "UjKbMcbV1ep3t+qANjOWFzxbyjmFn70NXlCbcJ07+EKiK9iabsFgagzXAoGAJ7r0\n" +
            "gcu4cVMRfHlJfQCjStQ2iu29m/n0F3DKsb6YkteZpqai0P3Xy36Gzs3DJeACWBcH\n" +
            "US9V2aGdVt9x/5YJ5p78hT6rsU/rkf3ilK4JltqXJRVCxIj4OD1cvazcYdqkgp9G\n" +
            "9tI8U7lHkfDBpj3HTSxvFpaC56qOjzowF9JzKYECgYBKTQNLOE0yHkj/JELwbHXi\n" +
            "sF0U8mv+aKtKqFY1xYxtbmnG3DcpmzETmSJloSl7X0vPB5mKpgOLax2698fqN0d/\n" +
            "OZdWTKp9Jr72HFT41vh/tw7aHr0hlf+/b2kqLGYnlpaKtAyl+E/rtQYzGNCcpNuS\n" +
            "ERJihYuM5ACj9/JC2y1aWg==\n" +
            "-----END PRIVATE KEY-----\n");


    /**
     * JSAPI支付下单，并返回JSAPI调起支付数据
     * @throws IOException
     */
    public static PrepayWithRequestPaymentResponse prepayWithRequestPayment(PrepayRequest request) throws IOException {
        // 初始化商户配置
        Config config =
                new RSAAutoCertificateConfig.Builder()
                        .merchantId(merchantId)
                        // 使用 com.wechat.pay.java.core.util 中的函数从本地文件中加载商户私钥，商户私钥会用来生成请求的签名
                        .privateKey(merchantPrivateKey)
                        .merchantSerialNumber(merchantSerialNumber)
                        .apiV3Key(apiV3Key)
                        .build();
        // 初始化服务
        service =
                new JsapiServiceExtension.Builder()
                        .config(config)
                        .signType("RSA") // 不填默认为RSA
                        .build();
        // 调用接口
        request.setAppid(appId);
        request.setMchid(merchantId);
        return service.prepayWithRequestPayment(request);
    }


    /** 关闭订单
     * @throws IOException
     */
    public static void closeOrder(CloseOrderRequest request) throws IOException {
        // 初始化商户配置
        Config config =
                new RSAAutoCertificateConfig.Builder()
                        .merchantId(merchantId)
                        // 使用 com.wechat.pay.java.core.util 中的函数从本地文件中加载商户私钥，商户私钥会用来生成请求的签名
                        .privateKey(merchantPrivateKey)
                        .merchantSerialNumber(merchantSerialNumber)
                        .apiV3Key(apiV3Key)
                        .build();
        // 初始化服务
        service =
                new JsapiServiceExtension.Builder()
                        .config(config)
                        .signType("RSA") // 不填默认为RSA
                        .build();
        // 调用接口
        request.setMchid(merchantId);
        service.closeOrder(request);
    }

    /** 微信支付订单号查询订单
     * @throws IOException
     */
    public static Transaction queryOrderById(QueryOrderByIdRequest request) throws IOException {
        // 初始化商户配置
        Config config =
                new RSAAutoCertificateConfig.Builder()
                        .merchantId(merchantId)
                        // 使用 com.wechat.pay.java.core.util 中的函数从本地文件中加载商户私钥，商户私钥会用来生成请求的签名
                        .privateKey(merchantPrivateKey)
                        .merchantSerialNumber(merchantSerialNumber)
                        .apiV3Key(apiV3Key)
                        .build();
        // 初始化服务
        service =
                new JsapiServiceExtension.Builder()
                        .config(config)
                        .signType("RSA") // 不填默认为RSA
                        .build();
        // 调用接口
        request.setMchid(merchantId);
        return service.queryOrderById(request);
    }

    /** 商户订单号查询订单
     * @throws IOException
     */
    public static Transaction queryOrderByOutTradeNo(QueryOrderByOutTradeNoRequest request) throws IOException {
        // 初始化商户配置
        Config config =
                new RSAAutoCertificateConfig.Builder()
                        .merchantId(merchantId)
                        // 使用 com.wechat.pay.java.core.util 中的函数从本地文件中加载商户私钥，商户私钥会用来生成请求的签名
                        .privateKey(merchantPrivateKey)
                        .merchantSerialNumber(merchantSerialNumber)
                        .apiV3Key(apiV3Key)
                        .build();
        // 初始化服务
        service =
                new JsapiServiceExtension.Builder()
                        .config(config)
                        .signType("RSA") // 不填默认为RSA
                        .build();
        // 调用接口
        request.setMchid(merchantId);
        return service.queryOrderByOutTradeNo(request);
    }


}
